#!/usr/bin/python
import socket, traceback, os, sys, time, struct
import qutils,qcfg

HTCP_SERVER_TYPE_WATER 	= 0x07
HTCP_WATER_CMD_START    = 0x79
HTCP_WATER_CMD_STOP     = 0x78
HTCP_WATER_CMD_STATUS   = 0x77

srvaddr = (qcfg.HTCP_WATER_SRV_IP, qcfg.HTCP_WATER_SRV_PORT)

def cmdSend(aCmd):
	try:
		qutils.htprint("REQ", aCmd)
		sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		sock.connect(srvaddr)
		sock.send(aCmd)
		recv = sock.recv(1024)
		qutils.htprint("RSP", recv)
		return recv
	except socket.gaierror, e:
	    print e
	except socket.error, e:
	    print e
	return ''

def cmdStart(aUid, aDid) :
	print "-"*50
	print "CMD-START"
	tmpUid = str(aUid)
	tmpDid = str(aDid)
	tmpLen = len(tmpUid) + len(tmpDid)
	cmdBuf = struct.pack('!4sBBB', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_START, tmpLen)
	cmdBuf += struct.pack('!%ds' % (len(tmpDid)), tmpDid)
	cmdBuf += struct.pack('!%dsBB' % (len(tmpUid)), tmpUid, 0, 0)
	recBuf = cmdSend(cmdBuf)
	if len(recBuf) == 9 :
		print "CMD-START-SUCCESS"
		return 0
	print "CMD-STOP-FAILURE"
	return -1
   	
def cmdStop(aUid, aDid) :
	print "-"*50
	print "CMD-STOP"
	tmpUid = str(aUid)
	tmpDid = str(aDid)
	tmpLen = len(tmpUid) + len(tmpDid)
	#cmdBuf = struct.pack('!4sBBB16s', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_STOP, tmpLen, tmpDid)
	cmdBuf = struct.pack('!4sBBB', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_STOP, tmpLen)
	cmdBuf += struct.pack('!%ds' % (len(tmpDid)), tmpDid)
	cmdBuf += struct.pack('!%dsBB' % (len(tmpUid)), tmpUid, 0, 0)
	recBuf = cmdSend(cmdBuf)
	if len(recBuf) == 9 :
		print "CMD-STOP-SUCCESS"
		return 0
	print "CMD-STOP-FAILURE"
	return -1
    
def cmdStatus(aUid, aDid) :
	print "-"*50
	print "CMD-STATUS"
	tmpUid = str(aUid)
	tmpDid = str(aDid)
	tmpLen = len(tmpUid) + len(tmpDid)
	cmdBuf = struct.pack('!4sBBB', "HTCP", HTCP_SERVER_TYPE_WATER, HTCP_WATER_CMD_STATUS, tmpLen)
	cmdBuf += struct.pack('!%ds' % (len(tmpDid)), tmpDid)
	cmdBuf += struct.pack('!%dsBB' % (len(tmpUid)), tmpUid, 0, 0)
	recBuf = cmdSend(cmdBuf)
	if len(recBuf) != 14 :
		print "CMD-STATUS-ERROR"
		return -1
	head,srv,act,len1,status,val,crc = struct.unpack("!4sBBBBfH", recBuf)
	print "status=%d,val=%f" % (status, val)
	print "CMD-STATUS-SUCCESS"
	return str(status) + "|" + str(val)

if __name__ == '__main__':
	print "**************************************************************************"
	print "********************                                   *******************"
	print "********************     WaterTest For Dev v1.0.0      *******************"
	print "********************                                   *******************"
	print "********************           Tommy@2016              *******************"
	print "********************                                   *******************"
	print "**************************************************************************"
	
	print "SRV-IP=%s,SRV-PORT=%d" % (qcfg.HTCP_WATER_SRV_IP, qcfg.HTCP_WATER_SRV_PORT)
	cmdStart('1', '5730303148FF6B067266545327200667')
	#cmdStop('1', '1234567890654321')
	#print cmdStatus('1', '1234567890654321')
	